//
//  UIView+AlertView.m
//  DS_PJ_01
//
//  Created by admin on 16/8/18.
//  Copyright © 2016年 zhiqiyun. All rights reserved.
//

#import "UIView+AlertView.h"
#import "DSMacros.h"

typedef void(^sAlertBlock)(UIAlertAction *action,NSInteger index);


@implementation UIView(AlertView)

-(void)showAlert:(NSString *)message controller:(UIViewController *)controller
{
    [self showAlert:message controller:controller handler:nil buttonArray:@[NSLocalizedString(@"OK", @"")]];
}


-(void)showAlert:(NSString *)message controller:(UIViewController *)controller handler:(sAlertBlock)block buttonArray:(NSArray *)nameArray;
{
    
    if (version_before(8)) {
        
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        
        [[[UIAlertView alloc] initWithTitle:nil
                                    message:message
                                   delegate:controller
                          cancelButtonTitle:NSLocalizedString(@"OK", @"")
                          otherButtonTitles:nil] show];
        
        NSAssert(controller, @"请在controller类实现UIAlertView的代理方法(若不需要适配iOS8以下则不需要实现)");
        
    } else {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
        
        int i = 0;
        
        for (NSString *name in nameArray) {
            
            NSAssert(![name isKindOfClass:[NSString class]], @"nameArray数组内元素请设置为NSString类型");
            
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(name, @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                block(action,i);
            
            }];
            [alertController addAction:okAction];
            
            i++;
        }
        [controller presentViewController:alertController animated:YES completion:nil];
        
    }

    
}

@end
